package com.password.desktop.UI;

import java.awt.Color;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;

import javax.swing.JFrame;

import com.password.desktop.util.SysUtil;

/**
 * 可移动的窗口基础类
 * @author yzh
 * @date 2020年6月4日
 *
 */
public abstract class AbstractMovedWindow extends JFrame implements MouseListener, MouseMotionListener {
	public final Point origin = new Point();// 变量，用于可拖动窗体

	@Override
	public void mouseDragged(MouseEvent e) {
		if (e.getSource() == this) {
			// 拖拽移动位置
			Point p = getLocation();
			setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y);
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		if (e.getSource() == this) {
			// 记录点击的位置
			origin.x = e.getX();
			origin.y = e.getY();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	}

	public URL getResoure(String path) {
		return this.getClass().getClassLoader().getResource(path);
	}

	public void initSet() {
		getContentPane().setLayout(null);
		getContentPane().setBackground(new Color(202, 202, 202));
		this.setResizable(false);
		this.setIconImage(Toolkit.getDefaultToolkit().createImage(SysUtil.getResoure("images/login/logo.png")));// 窗体图标
		this.setLocationRelativeTo(null);// 保持居中
		this.setTitle("PM guest");
		this.setUndecorated(true);// 去顶部
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}
}
